
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "RTKeyframeController.h"

namespace RT
{

	Keyframe::Keyframe(F32 position, F32 time)
	:_position(position), _time(time)
	{}

	RTKeyframeController::RTKeyframeController()
	:m_easyinout(false)
	{
		m_time.init(this, 0);
		registerDependent(&m_time);
	}

	RTKeyframeController::~RTKeyframeController()
	{
		for(vector<Keyframe*>::iterator i = m_keyframeList.begin(); i != m_keyframeList.end(); i++)
		{
			if( *i != 0)
			{
				delete *i;
				*i = 0;
			}
		}
	}

	void RTKeyframeController::addKeyframe(Keyframe* s)
	{
		U32 i=0;
		bool add=false;

		// if i < to the size of the vector and the keyframe has not been added
		while (i < m_keyframeList.size() && add==false)
		{
			//if the time of the keyframe in the vector is > to the time of the new keyframe
			if(m_keyframeList[i]->_time > s->_time )
			{
				m_keyframeList.insert(m_keyframeList.begin()+i, s);
				add=true;
			}
			i++;
		}
		//if the keyframe has not been added we put it in the end of the vector
		if(add==false)
		{
			m_keyframeList.push_back( s );
		}
	}

	void RTKeyframeController::doUpdate()
	{
		F32 s=0;
		F32 p=0;
		U32 i=0;
		bool found = false;
	

		if (m_time>= m_keyframeList[0]->_time && m_time<m_keyframeList[m_keyframeList.size()-1]->_time)
		{
			while(i<m_keyframeList.size() && found==false)
			{
				if(m_keyframeList[i]->_time>m_time)
				{
					found=true;
				}
				else
				{
					i++;
				}
			}
			//i is now the index of the second keyframe
			s = (m_time-m_keyframeList[i-1]->_time)/(m_keyframeList[i]->_time-m_keyframeList[i-1]->_time);
			if(m_easyinout==true)
			{
				s=3*s*s-2*s*s*s;
			}
			p = m_keyframeList[i-1]->_position + (m_keyframeList[i]->_position-m_keyframeList[i-1]->_position)*s;
			setValue(p);
		}
	}

}